package com.sand.service.Imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sand.dao.IBaseDao;
import com.sand.entity.CollectionEntity;
import com.sand.service.ICollectionService;
import com.sand.util.PageModel;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@NoArgsConstructor
@Data
@Service(value = "collectionService")
public class CollectionServiceImpl implements ICollectionService {

    /**
     *
     *
     * @author <achao1441470436@gmail.com>
     * @since 2021/12/1/001 9:11
     */
    //Dao层接口
    @Resource(name = "baseDao")
    private IBaseDao baseDao;

    @Override
    public PageModel<HashMap<String, Object>> queryCollectionByUserIdLimit(int limit, int page, String condition) {
        PageModel<HashMap<String, Object>> pm = new PageModel<>();
        //分页开始的位置
        page = (page-1)*limit;
        //创建查询对象
        DetachedCriteria criteria = DetachedCriteria.forClass(CollectionEntity.class);
        if(condition!=null){
            JSONObject obj = JSON.parseObject(condition);
            if (obj.get("userId")!=null) {
                List list = baseDao.queryObject("select t1.rid  from CollectionEntity t1 where t1.userId = ?0 ",Integer.parseInt(obj.get("userId").toString()));
                criteria.add(Restrictions.in("rid", list));
            }

        }

        List<Long> list1 = baseDao.queryObjectCommon(criteria.setProjection(Projections.rowCount()));

        criteria.setProjection(null);

        //rid的集合
        criteria.setProjection(Projections.property("rid"));

        criteria.addOrder(Order.desc("rid"));

        //查询到所有
        List list = baseDao.queryObjectLimit(criteria, page, limit);

        long i=list1.get(0);

        int count= (int) i;

        List<HashMap<String, Object>> list2 = baseDao.queryObject(" select new Map(" +
                "t1.rid as rid,t1.price as rentalPrice,t1.rentalType as rentalType," +
                "t1.houseType as houseType,t1.towards as towards," +
                "t1.renovationCondition as renovationCondition,t5.communityName as communityName," +
                "t5.communityAddress as communityAddress,t4.housePicture as housePicture," +
                "t3.browseNumber as browseNumber,t6.name as name)" +
                "from HouseEntity t1 inner join CollectionEntity t2 on t2.houseId=t1.rid " +
                "inner join HouserecordEntity t3 on t3.houseId=t1.rid " +
                "inner join HouseattachmentEntity t4 on t4.houseId=t1.rid " +
                "inner join CommunityEntity t5 on t5.rid=t1.communityId " +
                "inner join CityEntity t6 on t6.rid=t5.cityId " +
                "where t4.isHouse=0 and t2.rid in (?0) group by t1.rid", list);


        pm.setList(list2);
        pm.setPageNo(page);
        pm.setPageSize(limit);
        pm.setPagecount(count);

        return pm;
    }
}
